package com.hdey.service.max.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdey.mapper.max.TbUscWarehouseMapper;
import com.hdey.pojo.max.TbUscWarehouse;
import com.hdey.service.max.TbUscWarehouseSvs;
import com.hdey.vo.WarehouseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service("tbUscWarehouseSvs")
@Transactional
@Slf4j
public class TbUscWarehouseSvsImpl extends ServiceImpl<TbUscWarehouseMapper, TbUscWarehouse> implements TbUscWarehouseSvs {

    @Resource
    private TbUscWarehouseMapper warehouseMapper;


    @Override
    public List<WarehouseVo> getTbUscWarehouse() {

        List<TbUscWarehouse> houseList = warehouseMapper.selectList(null);

        //System.out.println("仓库个数"+houseList.size());

        //东院区处理
        WarehouseVo eastWarehouse = new WarehouseVo();
        eastWarehouse.setTitle("东院区");

        WarehouseVo eastFirstWarehouse = new WarehouseVo();
        eastFirstWarehouse.setTitle("一级库");

        List<WarehouseVo>eastFirstChildren = new ArrayList<>();
        eastFirstWarehouse.setChildren(eastFirstChildren);

        WarehouseVo eastSecondWarehouse = new WarehouseVo();
        eastSecondWarehouse.setTitle("二级库");

        List<WarehouseVo> eastSecondChildren = new ArrayList<>();
        eastSecondWarehouse.setChildren(eastSecondChildren);

        //西院区处理
        WarehouseVo wastWarehouse = new WarehouseVo();
        wastWarehouse.setTitle("西院区");

        WarehouseVo wastFirstWarehouse = new WarehouseVo();
        wastFirstWarehouse.setTitle("一级库");

        List<WarehouseVo> wastFirstChildren = new ArrayList<>();
        wastFirstWarehouse.setChildren(wastFirstChildren);

        WarehouseVo wastSecondWarehouse = new WarehouseVo();
        wastSecondWarehouse.setTitle("二级库");

        List<WarehouseVo> wastSecondChildren = new ArrayList<>();
        wastSecondWarehouse.setChildren(wastSecondChildren);


        for(TbUscWarehouse warehouse : houseList){
            WarehouseVo house = new WarehouseVo();
            house.setId(warehouse.getWarehousId());
            house.setTitle(warehouse.getWarehouseName());
            house.setBranchCode(warehouse.getBranchCode());
            house.setDeleteFlag(warehouse.getDeleteFlag());
            house.setWarehouseCode(warehouse.getWarehouseCode());
            house.setWarehouseClass(warehouse.getWarehouseClass());

            if("东".equals(warehouse.getBranchCode())){
                if("1".equals(warehouse.getWarehouseClass())){
                    eastFirstChildren.add(house);
                }else if("2".equals(warehouse.getWarehouseClass())){
                    eastSecondChildren.add(house);
                }
            }else if("西".equals(warehouse.getBranchCode())){
                if("1".equals(warehouse.getWarehouseClass())){
                    wastFirstChildren.add(house);
                }else if("2".equals(warehouse.getWarehouseClass())){
                    wastSecondChildren.add(house);
                }
            }
        }

        List<WarehouseVo> eastWarehouseChildren = new ArrayList<>();

        eastWarehouseChildren.add(eastFirstWarehouse);
        eastWarehouseChildren.add(eastSecondWarehouse);

        eastWarehouse.setChildren(eastWarehouseChildren);


        List<WarehouseVo> wastWarehouseChildren = new ArrayList<>();

        wastWarehouseChildren.add(wastFirstWarehouse);
        wastWarehouseChildren.add(wastSecondWarehouse);

        wastWarehouse.setChildren(wastWarehouseChildren);


        List<WarehouseVo> list = new ArrayList<>();

        list.add(eastWarehouse);
        list.add(wastWarehouse);
        return list;
    }

    @Override
    public int updateTbUscWarehouse(TbUscWarehouse house) {
        house.setUpdateBy("admin");
        house.setUpdateDate(LocalDate.now());
        int result = warehouseMapper.updateById(house);
        return result;
    }

    @Override
    public int createTbUscWarehouse(TbUscWarehouse house) {
        house.setCreateBy("admin");
        house.setCreateDate(LocalDate.now());
        int result = warehouseMapper.insert(house);
        return result;
    }


    @Override
    public List<TbUscWarehouse> getTbUscWarehouseByBranch(String branchCode) {
      QueryWrapper<TbUscWarehouse> wrapper = new  QueryWrapper<>();
      wrapper.select("WAREHOUS_ID","WAREHOUSE_NAME")
                .eq("BRANCH_CODE",branchCode);
      List<TbUscWarehouse> list = warehouseMapper.selectList(wrapper);
      return list;
    }
}
